package hy.planegame.sprite

import android.graphics.Canvas
import hy.planegame.utils.bullet
import hy.planegame.utils.paint

/**
 * Created time : 2017/9/1 9:39.
 * @author HY
 */
class Bullet(val centerX: Int, private val bottom: Int) : Sprite<Bullet>(), Comparable<Bullet> {
    override fun compareTo(other: Bullet): Int = centerX * bottom - other.centerX * other.bottom

     val width = bullet.width
     val height = bullet.height

    private var isDraw = true
    private val speed = 10
    var y = bottom - height.toFloat()


    override fun draw(canvas: Canvas?) {
        canvas ?: return
        if (isDraw) {
            canvas.drawBitmap(bullet, centerX - width / 2f, y, paint)
        }
    }

    override fun logic() {
        if (isDraw) {
            if (y + height <= 0) {
                destroy()
            } else {
                y -= speed
            }
        }
    }

    private var _onDestroy: (() -> Unit)? = null
    fun onDestroy(callback: () -> Unit) {
        _onDestroy = callback
    }

    override fun destroy() {
        isDraw = false
        _onDestroy?.invoke()
    }
}